package org.myOrg.spetrum.GUI;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import org.myOrg.spetrum.io.InputManager;
import org.myOrg.spetrum.util.Message;
import org.myOrg.spetrum.util.MessageImplement;
import org.myOrg.spetrum.util.MessageMangement;
import org.myOrg.spetrum.util.MessageMangementImplement;
import org.myOrg.spetrum.util.OverallFactory;
import org.myOrg.spetrum.util.Reciver;
import org.myOrg.spetrum.util.Sender;
import org.myOrg.spetrum.util.Transform;

public class BugInsertPanel extends JPanel implements ActionListener, Reciver ,Sender{
	private static final long serialVersionUID = 1L;

	private JButton add;
	private JButton delete;
	private JList list;
	private BugInsertControlPanel hidePanel;
	private MessageMangement mangerment;
	
	public BugInsertPanel() {
		this.setMessageMangement(OverallFactory.getMessageManger());
		this.register(MessageMangementImplement.RUN,
				OverallFactory.getMessageManger());
		this.register(MessageMangementImplement.ADDBUGMODEL,
				OverallFactory.getMessageManger());
		this.register(MessageMangementImplement.READOVER,
				OverallFactory.getMessageManger());
		this.register(MessageMangementImplement.NOBUGMODEL,
				OverallFactory.getMessageManger());
		
		this.bulid();
		this.setRelative();
	}

	private void bulid() {

		list = new JList(new DefaultListModel());
		JScrollPane scrollPane = new JScrollPane(list);

		JPanel southPanel = new JPanel();
		southPanel.setLayout(new BorderLayout());

		JPanel buttonPanel = bulidButtonPanel();
		hidePanel = new BugInsertControlPanel();

		southPanel.add(buttonPanel, BorderLayout.NORTH);
		southPanel.add(hidePanel, BorderLayout.SOUTH);

		JLabel label = new JLabel("insert the bug model");
		this.setLayout(new BorderLayout());
		this.add(scrollPane, BorderLayout.CENTER);
		this.add(southPanel, BorderLayout.SOUTH);
		this.add(label, BorderLayout.NORTH);
	}

	private JPanel bulidButtonPanel() {
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new BorderLayout());

		JPanel control = new JPanel();
		control.setLayout(new FlowLayout(FlowLayout.LEADING));

		add = new JButton("add");
		add.setEnabled(false);
		control.add(add);

		delete = new JButton("delete");
		delete.setEnabled(false);
		control.add(delete);

		buttonPanel.add(control, BorderLayout.WEST);

		JPanel okPanel = new JPanel();

		buttonPanel.add(okPanel, BorderLayout.EAST);
		return buttonPanel;
	}

	public void setRelative() {
		add.addActionListener(this);
		delete.addActionListener(this);
	}

	public static void main(String[] args) {

		JFrame frame = new JFrame();
		BugInsertPanel bugModel = new BugInsertPanel();
		frame.setSize(560, 300);
		frame.setVisible(true);
		frame.getContentPane().add(bugModel);
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}
	
	public void clear(){
		((DefaultListModel)this.list.getModel()).removeAllElements();
		this.delete.setEnabled(false);
	}

	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if (e.getActionCommand().equals("add")) {
			this.hidePanel.setVisible(true);
		} else if (e.getActionCommand().equals("delete")) {
			DefaultListModel model = (DefaultListModel) this.list.getModel();
			int index = this.list.getSelectedIndex();
			model.removeElementAt(index);
			
			Message msg = new MessageImplement(this,
					MessageMangementImplement.DELETEBUGMODEL, new Integer(index));
			this.sendMessage(msg);
		}

	}

	@Override
	public void register(int messageType, MessageMangement messageManger) {
		// TODO Auto-generated method stub
		messageManger.register(this, messageType);
	}

	@Override
	public void handleMessage(Message msg) {
		// TODO Auto-generated method stub
		if (msg.getType() == MessageMangementImplement.ADDBUGMODEL) {
			DefaultListModel model = (DefaultListModel) this.list.getModel();
			model.addElement(Transform.vectorToString((int[]) msg.getInfo()));
			this.delete.setEnabled(true);
		}
		else if(msg.getType() == MessageMangementImplement.READOVER){			
			InputManager input = (InputManager) msg.getInfo();
			if(input.isFormatPass()){
				this.clear();
				this.add.setEnabled(true);
			}
		}
		else if(msg.getType() == MessageMangementImplement.NOBUGMODEL){
			this.delete.setEnabled(false);
		}
		else if(msg.getType() == MessageMangementImplement.RUN){
			this.setVisible(false);
		}

	}

	@Override
	public void sendMessage(Message msg) {
		// TODO Auto-generated method stub
		mangerment.dispatchMeesage(msg);
	}

	@Override
	public void setMessageMangement(MessageMangement mangerment) {
		// TODO Auto-generated method stub
		this.mangerment = mangerment;
	}

}
